﻿using System;
using System.Windows.Forms;

namespace ViewCam
{
    public partial class MainForm : Form
    {
        #region Declarations
        ViewCamHelper viewcamModule;
        CameraForm camForm;
        AboutForm abtForm;
        HelpForm hlpForm;
        SettingsForm setForm;
        LoginForm logForm;
        #endregion

        #region Defaults Controls
        /// <summary>
        /// Initializes a new instance of the <see cref="MainForm"/> class.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the ExitToolsStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            this.Dispose();
            Application.Exit();
        }

        /// <summary>
        /// Handles the FormClosing event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="FormClosingEventArgs"/> instance containing the event data.</param>
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ExitToolsStripMenuItem_Click(sender, e);
        }

        /// <summary>
        /// Handles the Click event of the closeAllToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            this.btnSelRef.Enabled = true;
        }

        /// <summary>
        /// Handles the Click event of the btnCloseAllCams control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnCloseAllCams_Click(object sender, EventArgs e)
        {
            closeAllToolStripMenuItem_Click(sender, e);
        }

        /// <summary>
        /// Handles the Click event of the StatusBarToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        /// <summary>
        /// Handles the Click event of the leftBarToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void leftBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = leftBarToolStripMenuItem.Checked;
        }

        /// <summary>
        /// Handles the Load event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            viewcamModule = new ViewCamHelper();

            try
            {
                if (Convert.ToBoolean(viewcamModule.GetConfigSettings("MaxedStart", lblStatus)) == true)
                {
                    this.WindowState = FormWindowState.Maximized;
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception)
            {
                this.WindowState = FormWindowState.Normal;
            }

            this.BackColor = viewcamModule.SetThemeColor(lblStatus);
            this.Text = "View Cam";
            this.lblStatus.Text = "Ready";
            this.BringToFront();

            viewcamModule.LoadForm(this, this.BackColor);
            viewcamModule.LoadWebCam(cmbCamera);
        }

        /// <summary>
        /// Handles the Resize event of the MainForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void MainForm_Resize(object sender, EventArgs e)
        {
            try
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    if (Convert.ToBoolean(viewcamModule.GetConfigSettings("HideToTray", lblStatus)) == true)
                    {
                        this.Hide();
                    }
                    else
                    {
                        this.WindowState = FormWindowState.Minimized;
                    }
                }
            }
            catch (Exception)
            {  }
            
        }
        #endregion

        #region Modified Controls
        /// <summary>
        /// Handles the Click event of the btnLoadCam control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnLoadCam_Click(object sender, EventArgs e)
        {
            camForm = new CameraForm();
            viewcamModule = new ViewCamHelper();
            if (cmbCamera.SelectedText != "")
            {
                camForm.MdiParent = this;
                camForm.Title = cmbCamera.SelectedText;
                camForm.CamType = "WebCam";
                camForm.RecordPath = viewcamModule.GetConfigSettings("RecordPath", lblStatus);
                camForm.BeepFreq = Convert.ToInt32(viewcamModule.GetConfigSettings("Frequency", lblStatus));
                camForm.BeepDura = Convert.ToInt32(viewcamModule.GetConfigSettings("Duration", lblStatus));
                camForm.AutoRecordStop = Convert.ToInt32(viewcamModule.GetConfigSettings("AutoRecord", lblStatus));
                camForm.AutoNotifyStop = Convert.ToInt32(viewcamModule.GetConfigSettings("AutoNotify", lblStatus));
                camForm.CamOrder = cmbCamera.SelectedIndex;
                camForm.CamName = cmbCamera.SelectedText;
                camForm.cmbCamera = cmbCamera;

                viewcamModule.RemoveWebCam(cmbCamera, cmbCamera.SelectedText);
                viewcamModule.LoadStatusMessage("Camera Form Loaded.", lblStatus);

                camForm.Show();
                this.btnSelRef.Enabled = false;
            }
            else
            {
                viewcamModule.LoadStatusMessage("No Camera Selected.", lblStatus);
            }
        }

        /// <summary>
        /// Handles the Click event of the btnLoadIPCam control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnLoadIPCam_Click(object sender, EventArgs e)
        {
            logForm = new LoginForm();
            viewcamModule = new ViewCamHelper();

            logForm.title = "IP Cam Login Details";
            logForm.RecordPath = viewcamModule.GetConfigSettings("RecordPath", lblStatus);
            logForm.BeepFreq = Convert.ToInt32(viewcamModule.GetConfigSettings("Frequency", lblStatus));
            logForm.BeepDura = Convert.ToInt32(viewcamModule.GetConfigSettings("Duration", lblStatus));
            logForm.AutoRecordStop = Convert.ToInt32(viewcamModule.GetConfigSettings("AutoRecord", lblStatus));
            logForm.AutoNotifyStop = Convert.ToInt32(viewcamModule.GetConfigSettings("AutoNotify", lblStatus));
            logForm.mainForm = this;
            logForm.MdiParent = this;

            viewcamModule.LoadStatusMessage("IP Camera Login Form Loaded.", lblStatus);

            logForm.Show();
        }

        /// <summary>
        /// Handles the Click event of the aboutToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            viewcamModule = new ViewCamHelper();
            abtForm = new AboutForm();
            viewcamModule.LoadStatusMessage("About for View Cam is Loaded.", lblStatus);
            abtForm.ShowDialog();
        }

        /// <summary>
        /// Handles the Click event of the btnSelRef control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnSelRef_Click(object sender, EventArgs e)
        {
            viewcamModule = new ViewCamHelper();
            viewcamModule.LoadWebCam(cmbCamera);
        }

        /// <summary>
        /// Handles the Click event of the helpToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            viewcamModule = new ViewCamHelper();
            hlpForm = new HelpForm();
            viewcamModule.LoadStatusMessage("Help for View Cam is Loaded.", lblStatus);
            hlpForm.ShowDialog();
        }

        /// <summary>
        /// Handles the Click event of the btnMin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnMin_Click(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(viewcamModule.GetConfigSettings("HideToTray", lblStatus)) == true)
            {
                this.Hide();
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnMax control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnMax_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }

        /// <summary>
        /// Handles the Click event of the btnClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            ExitToolsStripMenuItem_Click(sender, e);
        }

        /// <summary>
        /// Handles the Click event of the settingsToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setForm = new SettingsForm();
            setForm.lblStatus = this.lblStatus;
            setForm.menuSettings = this.settingsToolStripMenuItem;
            setForm.MdiParent = this;
            setForm.Show();
            this.settingsToolStripMenuItem.Enabled = false;
        }

        #endregion

        #region Form Method
        /// <summary>
        /// Closes all forms.
        /// </summary>
        private void CloseAllForms()
        {
            viewcamModule = new ViewCamHelper();
            viewcamModule.CloseAllChildForms(this);
            viewcamModule.Dispose();
        }

        /// <summary>
        /// Handles the MouseHover event of the btnMin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnMin_MouseHover(object sender, EventArgs e)
        {
            btnMin.Text = "Minimize To Tray";
        }

        /// <summary>
        /// Handles the MouseLeave event of the btnMin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnMin_MouseLeave(object sender, EventArgs e)
        {
            btnMin.Text = "";
        }
        #endregion

        #region Notify Method
        private void tsmenuShow_Click(object sender, EventArgs e)
        {
            this.Show();
            this.BringToFront();
        }

        private void tsmenuClose_Click(object sender, EventArgs e)
        {
            ExitToolsStripMenuItem_Click(sender, e);
        }
        #endregion

    }
}
